<template>
  <a-modal
    :title="$t('新增')"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    :okText="$t('确定')"
    @cancel="handleCancel"
    :cancelText="$t('取消')"
    v-drag
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model :model="dataForm" ref="form" :rules="rules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('商品')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodsId">
              <a-select
                v-model="dataForm.goodsId"
                show-search
                :placeholder="$t('请输入商品编码/名称/条码')"
                :filter-option="false"
                :not-found-content="null"
                @search="handleGoodsSearch"
              >
                <a-select-option v-for="good in addGoodsList" :key="good.id" @click="changeGoods(good)">{{
                  good.name
                }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('商品原价')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="originalPrice">
              <a-input v-model="dataForm.originalPrice"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('预售价格')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="presalePrice">
              <a-input v-model="dataForm.presalePrice" :placeholder="$t('请输入')" @change="changeFinal"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('定金金额')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deposit">
              <a-input v-model="dataForm.deposit" :placeholder="$t('请输入')" @change="changeFinal"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('尾款金额')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="finalPay">
              <a-input v-model="dataForm.finalPay" :placeholder="$t('请输入')" :disabled="true"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('预售数量')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quantity">
              <a-input v-model="dataForm.quantity" :placeholder="$t('请输入')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { getAction } from '@/api/manage'

export default {
  name: 'AddPresaleGoodsModal',
  data() {
    return {
      width: 800,
      visible: false,
      dataForm: {
        goodsId: '',
        originalPrice: '',
        presalePrice: '',
        deposit: '',
        finalPay: '',
        quantity: '',
      },
      confirmLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      dataForm: {
        goodsId: '',
      },
      rules: {
        goodsId: [{ required: true, message: this.$t('请选择商品') }],
        originalPrice: [{ required: true ,message: this.$t('请输入'), }],
        presalePrice: [{ required: true, message: this.$t('请输入') }],
        deposit: [{ required: true, message: this.$t('请输入') }],
        finalPay: [{ required: true, message: this.$t('请输入') }],
        quantity: [{ required: true, message: this.$t('请输入') }],
      },
      addGoodsList: [],
    }
  },
  methods: {
    changeFinal(){
      if(this.dataForm.presalePrice){
        if(this.dataForm.deposit){
          let final = Number(this.dataForm.presalePrice) - Number(this.dataForm.deposit)
          let x = final.toString().split('.')[1]
          this.$set(this.dataForm, 'finalPay', x>1?final.toFixed(2):final.toFixed(x))
        }else{
          this.$set(this.dataForm, 'finalPay', this.dataForm.presalePrice)
        }
      }else{
        this.$set(this.dataForm, 'finalPay', 0)
      }
    },
    add() {
      this.visible = true
      this.dataForm = {
        sort:1,
        originalPrice: '',
        presalePrice: '',
        deposit: '',
        finalPay: '',
        quantity: '',
      }
      this.getGoodsList()
    },
    handleGoodsSearch(value) {
      this.getGoodsList(value)
    },
    changeGoods(good) {
      this.dataForm.originalPrice = good.price
      this.dataForm.goodsId_dictText = good.name
    },
    getGoodsList(value) {
      let that = this
      let querydata = {}
      querydata.pageNo = 1
      querydata.pageSize = 9999
      querydata.goodsInfo = value
      querydata.form = '1,2,3,4,5,8'
      getAction('/goods/bisGoodsInfor/list', querydata).then((res) => {
        if (res.success) {
          that.addGoodsList = res.result.records
        }
      })
    },
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit('ok', this.dataForm)
          this.close()
        }
      })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.visible = false
    },
  },
}
</script>
<style>
.batchForm .ant-form-item-label {
  width: 30%;
}
.batchForm .ant-form-item-control-wrapper {
  width: 62.5%;
}
</style>